package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.SystemColor;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.SellController;

import javax.swing.JTextArea;
import org.jdesktop.swingx.JXDatePicker;

public class PanelBan extends JPanel {
	private static final long serialVersionUID = -3495894494935097161L;
	private JTable table;
	private Vector<Vector<String>> data;
	private Vector<String> columnNames;
	private DefaultTableModel tableModel;
	private SellController control;
	private JTextField sum;
	private JTextField sumMoney;
	private JTextField sumThue;
	private JTextField tien;
	private JTextField hinhThuc;
	private JPanel panel;
	private JTextField textField;

	public PanelBan() {
		this.control = new SellController(this);
		this.setLayout(new BorderLayout(0, 0));

		JSplitPane splitPane = new JSplitPane();
		this.add(splitPane, BorderLayout.CENTER);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setResizeWeight(0.1);
		splitPane.setOneTouchExpandable(true);

		JPanel north = new JPanel();
		splitPane.setLeftComponent(north);
		north.setLayout(new BorderLayout(0, 0));

		JPanel detail = new JPanel();
		north.add(detail, BorderLayout.NORTH);
		detail.setBorder(new TitledBorder(null,
				"Chi ti\u1EBFt h\u00F3a \u0111\u01A1n", TitledBorder.LEADING,
				TitledBorder.TOP, null, SystemColor.textHighlight));
		FormLayout fl_detail = new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.BUTTON_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(87dlu;pref)"), }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, });
		detail.setLayout(fl_detail);

		JLabel lblMH = new JLabel("Mã HĐ");
		detail.add(lblMH, "1, 2, right, default");

		textField = new JTextField();
		detail.add(textField, "3, 2, fill, default");
		textField.setColumns(18);

		JLabel lblNgyLp = new JLabel("Ngày lập");
		detail.add(lblNgyLp, "5, 2, right, default");

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Kh\u00E1ch h\u00E0ng",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		north.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 7));

		JPanel khachHang = new JPanel();
		panel_2.add(khachHang, BorderLayout.NORTH);
		khachHang
				.setLayout(new FormLayout(new ColumnSpec[] {
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("max(77dlu;default):grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("max(15dlu;default)"),
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTnKhchHng = new JLabel("Tên KH");
		lblMH.setPreferredSize(lblTnKhchHng.getPreferredSize());

		JXDatePicker datePicker = new JXDatePicker();
		datePicker.getEditor().setBackground(Color.WHITE);
		datePicker.getEditor().setEditable(false);
		datePicker.getEditor().setColumns(18);
		detail.add(datePicker, "7, 2");
		khachHang.add(lblTnKhchHng, "1, 2, left, default");

		JTextField textField_4 = new JTextField();
		khachHang.add(textField_4, "3, 2, 5, 1, fill, default");
		textField_4.setColumns(10);

		JLabel lblaCh = new JLabel("Địa chỉ");
		khachHang.add(lblaCh, "1, 4, left, default");

		JTextField textField_5 = new JTextField();
		khachHang.add(textField_5, "3, 4, fill, default");
		textField_5.setColumns(10);

		JLabel lblinThoi = new JLabel("Điện thoại");
		khachHang.add(lblinThoi, "5, 4, left, default");

		JTextField textField_3 = new JTextField();
		khachHang.add(textField_3, "7, 4, fill, default");
		textField_3.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportBorder(new TitledBorder(null, "Ghi ch\u00FA",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.activeCaption));
		panel_2.add(scrollPane, BorderLayout.CENTER);

		JTextArea textArea = new JTextArea();
		scrollPane.setViewportView(textArea);

		panel = new JPanel();
		splitPane.setRightComponent(panel);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"),
				"Danh s\u00E1ch mua h\u00E0ng", TitledBorder.LEADING,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		panel.setLayout(new BorderLayout(0, 7));

		JScrollPane list = new JScrollPane();
		list.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		list.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		panel.add(list, BorderLayout.CENTER);

		// TODO table
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setFillsViewportHeight(true);
		columnNames = new Vector<String>(Arrays.asList("M\u00E3 h\u00E0ng",
				"T\u00EAn h\u00E0ng", "\u0110\u01A1n v\u1ECB t\u00EDnh",
				"Thu\u1EBF su\u1EA5t (%)", "S\u1ED1 l\u01B0\u1EE3ng",
				"\u0110\u01A1n gi\u00E1"));
		data = new Vector<Vector<String>>();
		tableModel = new DefaultTableModel(data, columnNames);
		table.setModel(new DefaultTableModel(new Object[][] { { null, null,
				null, null, null, null }, }, new String[] {
				"M\u00E3 h\u00E0ng", "T\u00EAn h\u00E0ng",
				"\u0110\u01A1n v\u1ECB t\u00EDnh", "Thu\u1EBF su\u1EA5t (%)",
				"S\u1ED1 l\u01B0\u1EE3ng", "\u0110\u01A1n gi\u00E1" }));
		// table.getColumnModel().getColumn(0).setPreferredWidth(45);
		// table.getColumnModel().getColumn(0).setMaxWidth(50);
		table.getTableHeader().setReorderingAllowed(false);
		table.setAutoCreateRowSorter(true);
		list.setViewportView(table);

		JPanel thanhToan = new JPanel();
		this.add(thanhToan, BorderLayout.SOUTH);
		thanhToan.setBorder(new TitledBorder(null, "Thanh to\u00E1n",
				TitledBorder.LEADING, TitledBorder.TOP, null,
				SystemColor.textHighlight));
		thanhToan
				.setLayout(new FormLayout(new ColumnSpec[] {
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("max(15dlu;default):grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"), }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTngSLng = new JLabel("Tổng số lượng");
		thanhToan.add(lblTngSLng, "1, 2, left, default");

		sum = new JTextField();
		sum.setColumns(2);
		thanhToan.add(sum, "3, 2, fill, default");

		JLabel lblTinThu = new JLabel("Tổng tiền");
		thanhToan.add(lblTinThu, "5, 2, left, default");

		sumMoney = new JTextField();
		thanhToan.add(sumMoney, "7, 2, fill, default");
		sumMoney.setColumns(10);

		JLabel lblTngCng = new JLabel("Tổng thuế");
		thanhToan.add(lblTngCng, "9, 2, left, default");

		sumThue = new JTextField();
		thanhToan.add(sumThue, "11, 2, fill, default");
		sumThue.setColumns(10);

		JLabel lblTinThanhTon = new JLabel("Tiền thanh toán");
		thanhToan.add(lblTinThanhTon, "1, 4, left, default");

		tien = new JTextField();
		thanhToan.add(tien, "3, 4, fill, default");
		tien.setColumns(10);

		JLabel lblTinThanhTon_1 = new JLabel("Thanh toán");
		thanhToan.add(lblTinThanhTon_1, "5, 4, right, default");

		hinhThuc = new JTextField();
		thanhToan.add(hinhThuc, "7, 4, 5, 1, fill, default");
		hinhThuc.setColumns(10);

		JLabel lblSTinBng = new JLabel("Số tiền bằng chữ");
		lblSTinBng.setVisible(false);
		thanhToan.add(lblSTinBng, "1, 6, left, default");

		JTextField chu = new JTextField();
		thanhToan.add(chu, "3, 6, 9, 1, fill, default");
		chu.setVisible(false);
		chu.setColumns(10);

		JToolBar toolBar = new JToolBar();
		toolBar.setOrientation(SwingConstants.VERTICAL);
		toolBar.setFloatable(false);
		panel.add(toolBar, BorderLayout.EAST);

		// TODO button add
		JButton btnThm = new JButton("");
		btnThm.setActionCommand("add");
		btnThm.addActionListener(control);
		toolBar.add(btnThm);
		btnThm.setFocusable(false);
		btnThm.setToolTipText("Thêm");
		btnThm.setIcon(new ImageIcon(getClass().getResource("/images/new.png")));

		JButton btnXaHt = new JButton("");
		btnXaHt.setActionCommand("refresh");
		btnXaHt.addActionListener(control);
		toolBar.add(btnXaHt);
		btnXaHt.setFocusable(false);
		btnXaHt.setToolTipText("Refresh");
		btnXaHt.setIcon(new ImageIcon(getClass().getResource(
				"/images/refresh.png")));

		JButton btnXa = new JButton("");
		btnXa.setActionCommand("remove");
		btnXa.addActionListener(control);
		toolBar.add(btnXa);
		btnXa.setFocusable(false);
		btnXa.setToolTipText("Xóa");
		btnXa.setIcon(new ImageIcon(getClass()
				.getResource("/images/delete.png")));

		JButton btnComp = new JButton("");
		btnComp.setActionCommand("comp");
		btnComp.addActionListener(control);
		btnComp.setToolTipText("Tính tiền");
		btnComp.setFocusable(false);
		btnComp.setIcon(new ImageIcon(PanelBan.class
				.getResource("/images/dollar.png")));
		toolBar.add(btnComp);

		JButton button = new JButton("");
		button.setActionCommand("save");
		button.addActionListener(control);
		button.setFocusable(false);
		button.setToolTipText("Lưu");
		button.setIcon(new ImageIcon(getClass().getResource("/images/save.png")));
		toolBar.add(button);
	}

	public JTable getTable() {
		return this.table;
	}

	public Vector<Vector<String>> getData() {
		return this.data;
	}

	public DefaultTableModel getTableModel() {
		return this.tableModel;
	}

	public Vector<String> getColumnNames() {
		return this.columnNames;
	}

	public void setValue(int sum, int sumMoney, double sumThue, int tien) {
		this.sum.setText(sum + "");
		this.sumMoney.setText(sumMoney + "");
		this.sumThue.setText(sumThue + "");
		this.tien.setText(tien + "");
	}

}
